package com.zx.oa.controller;

import com.zx.oa.entity.CheckJob;
import com.zx.oa.entity.ClassInfo;
import com.zx.oa.entity.Job;
import com.zx.oa.entity.Major;
import com.zx.oa.service.census.CensusService;
import com.zx.oa.service.census.CensusServicelpml;
import com.zx.oa.service.job.JobService;
import com.zx.oa.service.job.JobServiceImpl;
import com.zx.oa.utils.PageSupport;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * 作业完成情况录入Servlet
 */
@WebServlet("/jobServlet.do")
public class JobServlet extends HttpServlet {

    JobService jobService = new JobServiceImpl();
    CensusService censusService=new CensusServicelpml();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String caozuo = request.getParameter("param");
        if ("cha".equals(caozuo)) {
            chaServlet(request,response);
        } else if ("upset".equals(caozuo)) {
            upsetServlet(request,response);
        }
    }

    private void upsetServlet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time=df.format(new Date());// new Date()为获取当前系统时间
        Date ckTime = null;
        try {
            ckTime = df.parse(time);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        //设置响应到客户端的数据的编码格式
        response.setContentType("text/html;charset=utf-8");
        String result="";
        //得到增加页面输入的数据
        String ckStatu = request.getParameter("ckStatu");
        String[] studentIds = request.getParameter("studentId").split(","); //学号数组

        boolean flag = false;
        for(int i=0;i<studentIds.length;i++) {
            //封装到对象中
            int studentId = Integer.parseInt(studentIds[i]);
            CheckJob checkJob = new CheckJob(studentId,ckTime,ckStatu);
            flag = jobService.upsetJob(checkJob);
        }
        if (flag){ result = "exists"; }
        //将结果值响应给客户端，ajax就能根据返回的数据在success函数做业务处理
        PrintWriter out= response.getWriter();
        out.print(result);
        out.flush();
        out.close();
        request.getRequestDispatcher("jobServlet.do?param=cha").forward(request,response);
    }

    private void chaServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String major = null; //查询的专业
        String className = null; //查询的班级名称
        String queryMajor = request.getParameter("queryMajor");
        String queryClassName = request.getParameter("queryClassName");
        if(null!=queryMajor&&!"".equals(queryMajor)){
            major=queryMajor;
        }
        if(null!=queryClassName&&!"".equals(queryClassName)){
            className=queryClassName;
        }
        //获取到当前页号
        String pageIndex= request.getParameter("pageIndex");
        if(pageIndex==null){
            //第一次访问查询的servlet的时候没有pageIndex，赋默认值1
            pageIndex="1";
        }
        int currentPageNo=1;//当前页号
        int pageSize= PageSupport.pageSizeVal;//页面尺寸复制
        currentPageNo=Integer.parseInt(pageIndex);
        //获取到总记录数
        int totalCount= jobService.getJobTotalCount(major,className);
        //存储页面信息
        PageSupport pages=new PageSupport();
        //设置页面尺寸
        pages.setPageSize(pageSize);
        //设置页面总记录数（注意此方法计算得到总页面数量）
        pages.setTotalCount(totalCount);
        //获取到总的页数
        int totalPageCount=pages.getTotalPageCount();
        if(currentPageNo<1){
            currentPageNo=1;
        }else if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        //将当前页号存储到pages对象
        pages.setCurrentPageNo(currentPageNo);


        //获取到全部的培训专业
        List<Major> jobMajorList=censusService.getMajorAll();
        //获取到全部的班级
        List<ClassInfo> jobClassNameList=censusService.getClassAll(major);


        //多条件查询
//        List<Job> jobMajorList = jobService.getJobMajorList();
//        List<Job> jobClassNameList = jobService.getJobClassNameList();
        List<Job> jobList = jobService.getJobList(major,className,currentPageNo,pageSize);

        //把查询的的列表数据存储到session，方便execel到处用
        request.getSession().setAttribute("jobListSession",jobList);
        request.setAttribute("pageInfo",pages);//分页信息
        request.setAttribute("queryMajor",major);//查询条件培训专业，进行回显
        request.setAttribute("queryClassName",className);//查询条件班级名称，进行回显
        request.setAttribute("jobMajorList", jobMajorList);//查询专业
        request.setAttribute("jobClassNameList", jobClassNameList);//查询班级名称
        request.setAttribute("jobList", jobList);//考勤列表信息
        request.getRequestDispatcher("jsp/jobInput.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
